package com.jincp.common.base;

import android.os.Environment;
import android.os.Handler;
import android.support.multidex.MultiDexApplication;

import com.jincp.common.utils.MyLogger;

import java.io.File;

/**
 * Created by admin on 2017/3/17.
 */

public class BaseApplication extends MultiDexApplication {

    protected static String TAG;

    //以下属性应用于整个应用程序，合理利用资源，减少资源浪费
    private static BaseApplication nInstance;//上下文
    private static Thread mMainThread;//主线程
    private static long mMainThreadId;//主线程id
    private static Handler mMainHandler;//主线程Handler

    private static File cacheDir;

    public static File getAppCacheDir() {
        return cacheDir;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        TAG = getClass().getSimpleName();
        //对全局属性赋值
        nInstance = this;

        mMainThread = Thread.currentThread();
        mMainThreadId = android.os.Process.myTid();
        mMainHandler = new Handler(getMainLooper());

        /**
         * 如果存在SD卡则将缓存写入SD卡,否则写入手机内存
         */
        if (getApplicationContext().getExternalCacheDir() != null && ExistSDCard()) {
            cacheDir = getApplicationContext().getExternalCacheDir();
        } else {
            cacheDir = getApplicationContext().getCacheDir();
        }
        MyLogger.d(TAG, "path : " + cacheDir.getAbsolutePath());
    }

    public static BaseApplication getInstance() {
        return nInstance;
    }

    public static Thread getMainThread() {
        return mMainThread;
    }

    public static long getMainThreadId() {
        return mMainThreadId;
    }

    public static Handler getMainHandler() {
        return mMainHandler;
    }

    private boolean ExistSDCard() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }
}
